package com.cloud.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.mapper.BrandMapper;
import com.cloud.pojo.Brand;
import com.cloud.result.BusException;
import com.cloud.result.CodeEnum;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;

@DubboService
public class BrandServiceImpl implements BrandService {
  @Autowired
  private BrandMapper brandMapper;


  @Override
  public Brand findById(Long id){
    if (id == 0){
      int i = 1/0; // 模拟系统异常
     }else if (id == -1){
      throw new BusException(CodeEnum.PARAMETER_ERROR); // 模拟业务异常
     }
    return brandMapper.selectById(id);
   }


  @Override
  public List<Brand> findAll() {
    return brandMapper.selectList(null);
   }


  @Override
  public void add(Brand brand) {
    brandMapper.insert(brand);
   }


  @Override
  public void update(Brand brand) {
    brandMapper.updateById(brand);
   }


  @Override
  public void delete(Long id) {
    brandMapper.deleteById(id);
   }


  @Override
  public Page<Brand> search(Brand brand, int page, int size) {
    QueryWrapper<Brand> queryWrapper = new QueryWrapper();
    // 判断品牌名不为空
    if (brand != null && StringUtils.hasText(brand.getName())){
      queryWrapper.like("name",brand.getName());
     }
    Page<Brand> page1 = brandMapper.selectPage(new Page(page, size), queryWrapper);
    return page1;
   }
}
